package m.jclecai.com.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import m.jclecai.com.R;
import m.jclecai.com.adapter.BetChampionAdapter;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.RecordsBean;
import m.jclecai.com.constant.Constant_Result;
import m.jclecai.com.ui.JointPurchaseActivity;
import m.jclecai.com.utils.MyToast;
import m.jclecai.com.utils.http.FastOk;

/**
 * Created by zed on 2018/4/9.
 */

public class BettingChampionActivity extends BaseActivity implements View.OnClickListener {

    int pager = 0;
    String name;
    BetChampionAdapter adapter;
    List<RecordsBean> beans;
    @BindView(R.id.include_listview)
    ListView include_listview;
    @BindView(R.id.tv_bonus)
    TextView tv_bonus;
    @BindView(R.id.include_multiple_context)
    EditText include_multiple_context;
    @BindView(R.id.cb_saidan)
    CheckBox cb_saidan;
    @BindView(R.id.include_bet_way)
    TextView include_bet_way;
    @BindView(R.id.ll_bonus_optimize)
    LinearLayout ll_bonus_optimize;

    //倍数
    String multiple = "1";
    //预计奖金
    float minBonus, maxBonus = 0;

    @Override
    protected int getContentView() {
        return R.layout.activity_betting_jczq;
    }

    @Override
    protected void initView() {
        super.initView();
        Intent intent = getIntent();
        pager = intent.getIntExtra("pager", 0);
        name = intent.getStringExtra("name");
        String data = intent.getStringExtra("data");
        beans = new ArrayList<>();
        beans.addAll(FastOk.getList(data, RecordsBean.class));
        setTitle(name);
        leftOptionText("添加");
        rightOptionText("发起合买");
        cb_saidan.setVisibility(View.GONE);
        include_bet_way.setVisibility(View.GONE);
        ll_bonus_optimize.setVisibility(View.GONE);
//        rightOptionText("发起合买");
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new BetChampionAdapter(beans, this);
        adapter.setListener(new BetChampionAdapter.OnDelListener() {
            @Override
            public void del(int position) {
                beans.remove(position);
                adapter.notifyDataSetChanged();
                updateUI();
            }
        });
        View inflate = View.inflate(this, R.layout.lottery_list_footer, null);
        inflate.findViewById(R.id.betting_item_footer_clear).setOnClickListener(this);
        inflate.findViewById(R.id.betting_item_footer_check).setOnClickListener(this);
        include_listview.addFooterView(inflate);
        //低版本 addFooterView需要在setAdapter前面
        include_listview.setAdapter(adapter);
        updateUI();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.betting_item_footer_clear:
                if (beans.size() > 0)
                    showAlertDialog("提示", "确认清空所有数据?", "取消", "确认", null, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            beans.clear();
                            adapter.notifyDataSetChanged();
                            updateUI();
                        }
                    });
                break;
            case R.id.betting_item_footer_check:
                startActivity(LotteryProtocolActivity.getIntent(this));
                break;
        }
    }

    //过关倍数
    @OnTextChanged(R.id.include_multiple_context)
    void multiple(Editable s) {
        Log.e("bs", s.toString());
        if (TextUtils.isEmpty(include_multiple_context.getText().toString())) {
//            include_multiple_context.setText("1");
            return;
        }
        updateUI();
    }

    //支付
    @OnClick(R.id.toolbar_confirm)
    void pay() {
        if (TextUtils.isEmpty(include_multiple_context.getText().toString())) {
            include_multiple_context.setText("1");
            include_multiple_context.setSelection(1);
            updateUI();
        }

        if (beans.size() <= 0) {
            MyToast.showToast(getBaseContext(), "请选择投注球队");
            return;
        }
        /**
         * 此处提交之前先判断用户是否登录
         * 竞彩类 北京单场 只有混投调用balls  其余单关 混投奖金优化 其余奖金优化 其他栏目调用ball
         * 所以混合投注 做一个标识区分开减少判断逻辑
         */
        String stringCode = getBonus();
        Log.e("stringCode", stringCode);
        Intent intent = new Intent(this, LotteryPayActivity.class);
        intent.putExtra(PAY_PROJECETYPE, "0");
        intent.putExtra(PAY_MONEY, String.valueOf(beans.size() * 2 * Integer.valueOf(multiple)));
        intent.putExtra(PAY_MULTIPLE, multiple);
        intent.putExtra(PAY_VOTENUMS, String.valueOf(beans.size()));
        intent.putExtra(PAY_TYPE, pager == 0 ? "jcgj" : "jcgyj");
        intent.putExtra(PAY_CODES, stringCode);
        startActivityForResult(intent, Constant_Result.CLOSEPAGE);
    }

    //合买
    @OnClick(R.id.tv_right_option)
    void jointPurchase() {
        if (beans.size() < 2) {
            MyToast.showToast(this, "至少选择2场比赛");
            tv_bonus.setText("");
            return;
        }

        Integer multiple = 1;
        try {
            multiple = Integer.valueOf(this.multiple);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        Intent intent = new Intent(this, JointPurchaseActivity.class);
        intent.putExtra("totalNum", (beans.size()));
        intent.putExtra("multiple", multiple);
        intent.putExtra("totalMoney", (beans.size() * 2 * multiple));
        intent.putExtra("num", 1);
        intent.putExtra(PAY_TYPE, pager == 0 ? "jcgj" : "jcgyj");
        intent.putExtra(PAY_CODES, getBonus() + ";");//因为JointPurchaseActivity code会-1
        intent.putExtra(PAY_NAME, "合买");
        startActivityForResult(intent, Constant_Result.CLOSEPAGE);
    }

    String getBonus() {
        StringBuffer codes = new StringBuffer();
        for (int i = 0; i < beans.size(); i++) {
            codes.append(beans.get(i).getId());
            if (i < beans.size() - 1)
                codes.append(",");
        }
        return codes.toString();
    }

    void updateUI() {
        maxBonus = 0;
        minBonus = 0;
        Iterator<RecordsBean> iterator = beans.iterator();
        while (iterator.hasNext()) {
            RecordsBean next = iterator.next();
            float odds = Float.parseFloat(next.getOdds()) * 2;
            if (maxBonus <= odds)
                maxBonus = odds;
            if (minBonus == 0 || minBonus >= odds)
                minBonus = odds;
        }
        multiple = include_multiple_context.getText().toString().trim();
        int num = Integer.parseInt(multiple);
        String money = (num * minBonus + "~" + num * maxBonus);
        String txt = String.format(getString(R.string.jc_bonus_optimiiza), beans.size(), num, beans.size() * 2 * num, money);
        tv_bonus.setText(Html.fromHtml(txt));
    }

    @Override
    protected void close() {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        //返回上个页面同步数据
        Intent intent = new Intent();
        intent.putExtra("data", FastOk.string(beans));
        setResult(RESULT_OK, intent);
        finish();
    }
}
